<?php
	/**
	 * @package AmpleHarvest
	 */
	use\ampleharvest\code;

	/**
	 * @brief This file is used to Run all notifications that are due.
	 *
	 * - Revision History
	 *  - 0.1.1
	 * 	 - <b>Date</b>: 01 AUG 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - refactored code to work with new DB schema
	 *  - 0.1
	 * 	 - <b>Date</b>: 24 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1.1
	 * @ingroup code script classes
	 */
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/Tables/cTABLE_Saved_Queries.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/cScheduler.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/notification/query_Notification/cQueryNotification.php";
	require_once(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/search/cResults.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/search/solr/cSolr.php";
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cAmpleHarvestLog.php';

	require_once $codeConfig->C_STR_USER_MANAGER_PATH . '/Classes/cTABLE_Users.php';

	$obj_Now = new DateTime();
	$obj_MailNotification = null;
	$obj_Solr = null;
	$obj_Results = null;
	$obj_Template = null;
	$obj_TABLE_SavedQueries = new cTABLE_Saved_Queries();
	$str_IndexedDateKey = "index_date";

	$obj_TABLE_SavedQueries->clear_Query();
	$obj_TABLE_SavedQueries->add_ColumnQuery($obj_TABLE_SavedQueries->m_int_COL_NextRun, $obj_Now->format("Y-m-d H:i:s"), cCondition::c_str_OPERATOR_LESS);
	$obj_TABLE_SavedQueries->add_ColumnQuery($obj_TABLE_SavedQueries->m_int_COL_Schedule, 1);
	$int_Items = $obj_TABLE_SavedQueries->do_Search();

	ini_set("memory_limit", "100M");

	$m_obj_SolrServer = new ampleharvest\code\search\cResults();
	$obj_Log = new cAmpleHarvestLog();

	$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_INFO,
		"Starting Query Notification Process.",
		cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

	$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_INFO,
		"Processing $int_Items Queries.",
		cAmpleHarvestLog::C_INT_LEVEL_INFO);

	print("Processing $int_Items\n");

	foreach ($obj_TABLE_SavedQueries->get_ResultSet() as $obj_Query)
	{

		$obj_MailNotification = new cQueryNotification();

		$obj_TABLE_SavedQueries->load_DataFromArray($obj_Query);

		$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_INFO,
			"Processing Stored Query " . $obj_TABLE_SavedQueries->get_id() . ".",
			cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

		$m_obj_SolrServer->set_Solr(new \cSolr(null, null, null));
		$this->output_Error("Using default solr instance -- run_Notification");

		if (!$obj_TABLE_SavedQueries->get_q_string())
		{
			$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_ERROR,
				$obj_TABLE_SavedQueries->get_id() . " has no query string.",
				cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
			continue;
		}

		$obj_LastRun = new DateTime($obj_TABLE_SavedQueries->get_ColumnData($obj_TABLE_SavedQueries->m_int_COL_LastRun));

		$obj_Query = unserialize($obj_TABLE_SavedQueries->get_q_string());

		if (!isset($obj_Query->Query))
		{
			$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_ERROR,
				$obj_TABLE_SavedQueries->get_id() . " has no Query in its query string.",
				cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
			continue;
		}

		if (!$m_obj_SolrServer->set_QueryFromJson($obj_Query))
		{
			$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_ERROR,
				$obj_TABLE_SavedQueries->get_id() . " does not contain a vailid query object.",
				cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
			continue;
		}
		if (!empty($obj_LastRun))
		{
			$m_obj_SolrServer->add_Query(cSolr::C_STR_KEY_INDEX_DATE, "[" . $obj_LastRun->format(cSolr::C_STR_FORMAT_DATE) . " TO *]");
			//$str_Query = "(" . $str_Query . ") AND " . cWiki_Doc::C_STR_FIELD_INDEX_DATE . ":[" . $obj_LastRun->format(cWiki_Solr::C_STR_FORMAT_DATE) . " TO *]";
		}

		$m_obj_SolrServer->set_Page(0);

		$m_obj_SolrServer->set_PerPage($obj_TABLE_SavedQueries->get_max());

		$m_obj_SolrServer->set_DisplayFields(array("*", "score"));
		$m_obj_SolrServer->set_UserID($obj_TABLE_SavedQueries->get_user_id());

		ob_start();
		$m_obj_SolrServer->output_ResultsAsHTML();
		$str_Output = ob_get_clean();

		$obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_QUERY_NOTIFICATION_INFO,
			$obj_TABLE_SavedQueries->get_id() . " returned" . $m_obj_SolrServer->get_Total() . " results.",
			cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

		$obj_MailNotification->set_Body($str_Output);

		$obj_MailNotification->add_To(get_UserEmail($obj_TABLE_SavedQueries->get_ColumnData($obj_TABLE_SavedQueries->m_int_COL_UserID)));
		$obj_MailNotification->set_Subject("Query Results for - " . $obj_TABLE_SavedQueries->get_q_title() . " (" . $obj_Now->format("d/M/Y H:00") . ")");

		$obj_MailNotification->send();

		$obj_TABLE_SavedQueries->set_ColumnValue($obj_TABLE_SavedQueries->m_int_COL_LastRun, $obj_Now->format("Y-m-d H:i:s"));

		$obj_Scheduler = new cScheduler($obj_TABLE_SavedQueries->get_t_hour(),
			$obj_TABLE_SavedQueries->get_t_day(),
			$obj_TABLE_SavedQueries->get_t_dayofweek(),
			$obj_TABLE_SavedQueries->get_t_month());

		$obj_TABLE_SavedQueries->set_next_run($obj_Scheduler->get_Next()->format("Y-m-d H:i:s"));

		$obj_TABLE_SavedQueries->save_Data2DB();

		//$obj_TABLE_SavedQueries->output_Debug_Object_HTML();

	}

	function get_UserEmail($str_UserID)
	{
		$obj_TABLE_Users = new cTABLE_Users();

		$obj_TABLE_Users->load_DataFromUserID($str_UserID);

		return $obj_TABLE_Users->get_ColumnData($obj_TABLE_Users->m_int_COL_Email1);
	}

?>
	